package com.liu.qqRobot.myself.handler;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liu.common.entity.vo.QQImageOCRVO;
import com.liu.common.utils.QQClientUtils;
import com.liu.mybatisPlus.official.entity.base.OfficialItemTendency;
import com.liu.mybatisPlus.official.service.OfficialItemTendencyService;
import com.liu.mybatisPlus.q.service.QGroupUserOperationQuantityService;
import com.liu.mybatisPlus.wiki.entity.base.WikiRivenAttributeBasicValues;
import com.liu.mybatisPlus.wiki.entity.base.WikiRivenAttributeNameKeyWord;
import com.liu.mybatisPlus.wiki.service.WikiRivenAttributeBasicValuesService;
import com.liu.mybatisPlus.wiki.service.WikiRivenAttributeNameKeyWordService;
import com.liu.mybatisPlus.wm.entity.base.WmRivenItem;
import com.liu.mybatisPlus.wm.service.WmRivenItemKeyWordService;
import com.liu.mybatisPlus.wm.service.WmRivenItemService;
import com.liu.qqRobot.event.entity.dto.EventReportingDTO;
import com.liu.qqRobot.myself.enums.RivenBurdenCorrectionFactorEnum;
import com.liu.qqRobot.myself.enums.RivenStraightCorrectionFactorEnum;
import com.liu.qqRobot.q.enums.OperationTypeEnum;
import com.liu.qqRobot.wm.entity.dto.WmRivenItemDTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URL;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Slf4j
@Component
public class RaHandler {

    /**
     * 最大浮动系数
     */
    BigDecimal MAX_FLOAT_COEFFICIENT = new BigDecimal("1.1");

    /**
     * 中间浮动系数
     */
    BigDecimal MIDDLE_FLOAT_COEFFICIENT = new BigDecimal("1");

    /**
     * 最小浮动系数
     */
    BigDecimal MIN_FLOAT_COEFFICIENT = new BigDecimal("0.9");

    @Resource
    private WmRivenItemService wmRivenItemService;

    @Resource
    private OfficialItemTendencyService officialItemTendencyService;

    @Resource
    private WikiRivenAttributeBasicValuesService wikiRivenAttributeBasicValuesService;

    @Resource
    private QGroupUserOperationQuantityService qGroupUserOperationQuantityService;

    @Resource
    private WmRivenItemKeyWordService wmRivenItemKeyWordService;

    @Resource
    private WikiRivenAttributeNameKeyWordService wikiRivenAttributeNameKeyWordService;

    /**
     * 处理事件
     * @param msgList 信息内容
     * @param groupId 群号
     * @param userId 发送人QQ号
     */
    public void handler(List<String> msgList, Long groupId, Long userId, Boolean isGroup) {
        String rivenUrl = msgList.stream().filter(m -> !m.startsWith("ra")).findFirst().get();
        String itemName = msgList.stream().filter(m -> m.startsWith("ra")).findFirst().get().replace("ra", "").replace(" ", "");
        try {
            this.saveRivenImg(rivenUrl);
        } catch (IOException e) {
            log.error("保存QQ中图片到本地异常，异常原因：{}", e.getMessage(), e);
            return;
        }
        Map<String, Object> param = this.getParam(itemName);
        QueryWrapper<WmRivenItem> wmRivenItemQueryWrapper = new QueryWrapper<>();
        wmRivenItemQueryWrapper.like("item_name", param.get("rivenName"));
        List<WmRivenItem> wmRivenItemList = wmRivenItemService.list(wmRivenItemQueryWrapper);
        List<String> totalStatsNameList = new ArrayList<>();
        List<String> positiveStatsList = (List<String>) param.getOrDefault("positiveStatsList", new ArrayList<>());
        List<String> positiveStatsNameList = positiveStatsList.stream().map(p -> p.split(" ")[1]).collect(Collectors.toList());
        totalStatsNameList.addAll(positiveStatsNameList);
        Map<String, String> positiveValueMap = positiveStatsList.stream().collect(Collectors.toMap(p -> p.split(" ")[1], p -> p.split(" ")[0]));
        List<String> negativeStatsList = (List<String>) param.getOrDefault("negativeStatsList", new ArrayList<>());
        List<String> negativeStatsNameList = new ArrayList<>();
        Map<String, String> negativeValueMap = new HashMap<>();
        if (CollUtil.isNotEmpty(negativeStatsList)) {
            negativeStatsNameList = negativeStatsList.stream().map(p -> p.split(" ")[1]).collect(Collectors.toList());
            negativeValueMap = negativeStatsList.stream().collect(Collectors.toMap(p -> p.split(" ")[1], p -> p.split(" ")[0]));
        }
        totalStatsNameList.addAll(negativeStatsNameList);
        QueryWrapper<WikiRivenAttributeBasicValues> wikiRivenAttributeBasicValuesQueryWrapper = new QueryWrapper<>();
        wikiRivenAttributeBasicValuesQueryWrapper.in("name", totalStatsNameList);
        List<WikiRivenAttributeBasicValues> wikiRivenAttributeBasicValuesList = wikiRivenAttributeBasicValuesService.list(wikiRivenAttributeBasicValuesQueryWrapper);
        Map<String, WikiRivenAttributeBasicValues> wikiRivenAttributeBasicValuesMap = wikiRivenAttributeBasicValuesList.stream().collect(Collectors.toMap(WikiRivenAttributeBasicValues::getName, w -> w));
        if (wmRivenItemList.size() == 1) {
            WmRivenItem wmRivenItem = wmRivenItemList.get(0);
            int index = wmRivenItem.getUrlName().length();
            if (wmRivenItem.getUrlName().contains("_")) {
                index = wmRivenItem.getUrlName().indexOf("_");
            }
            String name = wmRivenItem.getUrlName().substring(0, 1).toUpperCase()
                    + wmRivenItem.getUrlName().substring(1, index);
            QueryWrapper<OfficialItemTendency> wmItemTendencyQueryWrapper = new QueryWrapper<>();
            wmItemTendencyQueryWrapper.like("name", name);
            List<OfficialItemTendency> wmItemTendencyList = officialItemTendencyService.list(wmItemTendencyQueryWrapper);
            Map<String, BigDecimal> correctionFactorMap = new HashMap<>();
            try {
                correctionFactorMap = this.getCorrectionFactor(positiveStatsList.size(), negativeStatsList == null ? 0:negativeStatsList.size());
            } catch (RuntimeException e) {
                QQClientUtils.sendMsg("[CQ:at,qq=" + userId + "]\n" + e.getMessage(), userId, groupId, isGroup);
            }
            BigDecimal positiveCorrectionFactor = correctionFactorMap.get("positiveCorrectionFactor");
            BigDecimal negativeCorrectionFactor = correctionFactorMap.get("negativeCorrectionFactor");
            StringBuilder sendMsg = new StringBuilder("[CQ:at,qq=" + userId + "]\n【" + param.get("rivenName") + "】分析如下：\n");
            for (OfficialItemTendency officialItemTendency : wmItemTendencyList) {
                sendMsg.append("【").append(officialItemTendency.getName()).append("】 ")
                        .append("倾向：").append(officialItemTendency.getTendency().setScale(2, RoundingMode.HALF_UP)).append("\n")
                        .append("正面属性：\n");
                positiveStatsNameList.forEach(positiveStatsName -> {
                    String positiveValue = positiveValueMap.get(positiveStatsName);
                    sendMsg.append(positiveStatsName).append("：").append(positiveValue).append("(");
                    WikiRivenAttributeBasicValues wikiRivenAttributeBasicValues = wikiRivenAttributeBasicValuesMap.get(positiveStatsName);
                    BigDecimal basicValue = null;
                    try {
                        basicValue = this.getBasicValue(wmRivenItem.getRivenType(), wikiRivenAttributeBasicValues);
                    } catch (RuntimeException e) {
                        QQClientUtils.sendMsg("[CQ:at,qq=" + userId + "]\n" + e.getMessage(), userId, groupId, isGroup);
                    }
                    BigDecimal minValue = this.cal(MIN_FLOAT_COEFFICIENT, basicValue, officialItemTendency.getTendency(), positiveCorrectionFactor).setScale(2, RoundingMode.HALF_UP);
                    BigDecimal middleValue = this.cal(MIDDLE_FLOAT_COEFFICIENT, basicValue, officialItemTendency.getTendency(), positiveCorrectionFactor).setScale(2, RoundingMode.HALF_UP);
                    BigDecimal maxValue = this.cal(MAX_FLOAT_COEFFICIENT, basicValue, officialItemTendency.getTendency(), positiveCorrectionFactor).setScale(2, RoundingMode.HALF_UP);
                    sendMsg.append(minValue).append("-").append(middleValue).append("-").append(maxValue).append(") 【");
                    if (middleValue.compareTo(new BigDecimal(positiveValue)) >= 0) {
                        sendMsg.append("低 ").append(middleValue.subtract(new BigDecimal(positiveValue)));
                    } else if (middleValue.compareTo(new BigDecimal(positiveValue)) < 0){
                        sendMsg.append("高 ").append(new BigDecimal(positiveValue).subtract(middleValue));
                    }
                    sendMsg.append("】\n");
                });
                sendMsg.append("负面属性：\n");
                Map<String, String> finalNegativeValueMap = negativeValueMap;
                negativeStatsNameList.forEach(negativeStatsName -> {
                    String negativeValue = finalNegativeValueMap.get(negativeStatsName);
                    sendMsg.append(negativeStatsName + "：").append(negativeValue).append("(");
                    WikiRivenAttributeBasicValues wikiRivenAttributeBasicValues = wikiRivenAttributeBasicValuesMap.get(negativeStatsName);
                    BigDecimal basicValue = null;
                    try {
                        basicValue = this.getBasicValue(wmRivenItem.getRivenType(), wikiRivenAttributeBasicValues);
                    } catch (RuntimeException e) {
                        QQClientUtils.sendMsg("[CQ:at,qq=" + userId + "]\n" + e.getMessage(), userId, groupId, isGroup);
                    }
                    BigDecimal minValue = BigDecimal.ZERO.subtract(this.cal(MIN_FLOAT_COEFFICIENT, basicValue, officialItemTendency.getTendency(), negativeCorrectionFactor).setScale(2, RoundingMode.HALF_UP));
                    BigDecimal middleValue = BigDecimal.ZERO.subtract(this.cal(MIDDLE_FLOAT_COEFFICIENT, basicValue, officialItemTendency.getTendency(), negativeCorrectionFactor).setScale(2, RoundingMode.HALF_UP));
                    BigDecimal maxValue = BigDecimal.ZERO.subtract(this.cal(MAX_FLOAT_COEFFICIENT, basicValue, officialItemTendency.getTendency(), negativeCorrectionFactor).setScale(2, RoundingMode.HALF_UP));
                    sendMsg.append(minValue).append("-").append(middleValue).append("-").append(maxValue).append(") 【");
                    if (middleValue.compareTo(new BigDecimal(negativeValue)) >= 0) {
                        sendMsg.append("低 ").append(middleValue.subtract(new BigDecimal(negativeValue)));
                    } else if (middleValue.compareTo(new BigDecimal(negativeValue)) < 0){
                        sendMsg.append("高 ").append(new BigDecimal(negativeValue).subtract(middleValue));
                    }
                    sendMsg.append("】\n");
                });
                sendMsg.append("----------------------------\n");
            }
            QQClientUtils.sendMsg(sendMsg.toString(), userId, groupId, isGroup);
            qGroupUserOperationQuantityService.statisticsOperationQuantityByType(groupId, userId, OperationTypeEnum.RA);
        }
    }

    /**
     * 保存图片
     * @param url url
     * @throws IOException 抛出异常
     */
    private void saveRivenImg(String url) throws IOException {
        String localUrl = "src/main/resources/rivenPicture/riven.png";
        File file = new File(localUrl);
        if (!file.exists()) {
            file.mkdirs();
        }
        InputStream inputStream = new URL(url).openConnection().getInputStream();
        BufferedImage bufferedImage = ImgUtil.read(inputStream);
        ImageIO.write(bufferedImage, "png", file);
    }

    /**
     * 解析参数
     * @return Map<String, String>
     */
    private Map<String, Object> getParam(String itemName) {
        Map<String, Object> result = new HashMap<>();
        Pattern r = Pattern.compile("[0-9.]+");
        List<WmRivenItem> wmRivenItems = wmRivenItemService.list();
        List<String> itemNameList = wmRivenItems.stream().map(WmRivenItem::getItemName).toList();
        List<WikiRivenAttributeBasicValues> wikiRivenAttributeBasicValues = wikiRivenAttributeBasicValuesService.list();
        List<String> attributeNameList = wikiRivenAttributeBasicValues.stream().map(WikiRivenAttributeBasicValues::getName).toList();
        List<WikiRivenAttributeNameKeyWord> wikiRivenAttributeNameKeyWordList = wikiRivenAttributeNameKeyWordService.list();
        Map<String, String> wikiRivenAttributeNameKeyWordMap = wikiRivenAttributeNameKeyWordList.stream().collect(Collectors.toMap(WikiRivenAttributeNameKeyWord::getKeyWord, WikiRivenAttributeNameKeyWord::getName));
        QQImageOCRVO qqImageOCRVO = QQClientUtils.OCR("src/main/resources/rivenPicture/riven.png");
        List<QQImageOCRVO.Data.Text> textList = qqImageOCRVO.getData().getTexts();
        List<String> positiveStatsList = new ArrayList<>();
        List<String> negativeStatsList = new ArrayList<>();
        for (QQImageOCRVO.Data.Text text : textList) {
            String words = text.getText().replaceAll(" ", "");
            if (itemName == null || itemName.equals("")) {
                for (String name : itemNameList) {
                    if (words.contains(name)) {
                        itemName = name;
                    }
                }
                if (itemName == null) {
                    List<WmRivenItemDTO> wmRivenItemDTOS = wmRivenItemKeyWordService.keyWordGetRiven();
                    for (WmRivenItemDTO wmRivenItemDTO : wmRivenItemDTOS) {
                        if (words.contains(wmRivenItemDTO.getKeyWord())) {
                            itemName = wmRivenItemDTO.getItemName();
                        }
                    }
                }
            }
            if (words.contains("+")) {
                this.initPositiveOrNegative(words, positiveStatsList, attributeNameList, wikiRivenAttributeNameKeyWordMap, r);
            } else if (words.contains("x")) {
                words = words.replace("x", "");
                Matcher wordsMatcher = r.matcher(words);
                String numericalValue = "";
                if (wordsMatcher.find()) {
                    numericalValue = wordsMatcher.group();
                }
                // 当x的值大于等于1的时候是正属性
                if (new BigDecimal(numericalValue).compareTo(BigDecimal.ONE) >= 0) {
                    this.initPositiveOrNegative(words, positiveStatsList, attributeNameList, wikiRivenAttributeNameKeyWordMap, r);
                } else {
                    this.initPositiveOrNegative(words, negativeStatsList, attributeNameList, wikiRivenAttributeNameKeyWordMap, r);
                }
            } else  {
                this.initPositiveOrNegative(words, negativeStatsList, attributeNameList, wikiRivenAttributeNameKeyWordMap, r);
            }
        }
        result.put("rivenName", itemName);
        result.put("positiveStatsList", positiveStatsList);
        result.put("negativeStatsList", negativeStatsList);
        return result;
    }

    /**
     * 处理正面属性
     * @param words words
     * @param statsList statsList
     * @param attributeNameList attributeNameList
     * @param wikiRivenAttributeNameKeyWordMap wikiRivenAttributeNameKeyWordMap
     * @param r r
     */
    private void initPositiveOrNegative(String words, List<String> statsList, List<String> attributeNameList,
                              Map<String, String> wikiRivenAttributeNameKeyWordMap, Pattern r) {
        words = words.replace("+", "").replace("-", "")
                .replace("x", "").replace("%", "");
        Matcher wordsMatcher = r.matcher(words);
        String numericalValue = "";
        if (wordsMatcher.find()) {
            numericalValue = wordsMatcher.group();
        }
        words = words.replace(numericalValue, "");
        // 由于wiki获取的属性名称与官方紫卡可能不一致，这里先匹配关键词转成wiki的名称
        if (wikiRivenAttributeNameKeyWordMap.containsKey(words)) {
            words = wikiRivenAttributeNameKeyWordMap.get(words);
        }
        if (attributeNameList.contains(words)) {
            statsList.add(numericalValue + " " + words);
        } else {
            for (String name : attributeNameList) {
                if (words.contains(name)) {
                    statsList.add(numericalValue + " " + name);
                    break;
                }
            }
        }
    }

    /**
     * 获取武器基础数值
     * @param rivenType rivenType
     * @param wikiRivenAttributeBasicValues wikiRivenAttributeBasicValues
     * @return BigDecimal
     */
    private BigDecimal getBasicValue(String rivenType, WikiRivenAttributeBasicValues wikiRivenAttributeBasicValues) {
        return switch (rivenType) {
            case "rifle" -> wikiRivenAttributeBasicValues.getRifle();
            case "shotgun" -> wikiRivenAttributeBasicValues.getShotgun();
            case "pistol" -> wikiRivenAttributeBasicValues.getPistol();
            case "melee" -> wikiRivenAttributeBasicValues.getCloseCombat();
            default -> throw new RuntimeException("解析属性基础数值错误");
        };
    }

    /**
     * 获取武器基础数值修正系数
     * @param positiveStatsQuantity positiveStatsQuantity
     * @param negativeStatsQuantity negativeStatsQuantity
     * @return Map<String, BigDecimal>
     */
    private Map<String, BigDecimal> getCorrectionFactor(Integer positiveStatsQuantity, Integer negativeStatsQuantity) {
        Map<String, BigDecimal> result = new HashMap<>();
        BigDecimal positiveCorrectionFactor = null;
        BigDecimal negativeCorrectionFactor = null;
        if (positiveStatsQuantity == 2 && negativeStatsQuantity == 0) {
            positiveCorrectionFactor = RivenStraightCorrectionFactorEnum.TWO_STRAIGHT_ZERO_BURDEN.getValue();
            negativeCorrectionFactor = RivenBurdenCorrectionFactorEnum.TWO_STRAIGHT_ZERO_BURDEN.getValue();
        } else if (positiveStatsQuantity == 2 && negativeStatsQuantity == 1) {
            positiveCorrectionFactor = RivenStraightCorrectionFactorEnum.TWO_STRAIGHT_ONE_BURDEN.getValue();
            negativeCorrectionFactor = RivenBurdenCorrectionFactorEnum.TWO_STRAIGHT_ONE_BURDEN.getValue();
        } else if (positiveStatsQuantity == 3 && negativeStatsQuantity == 0) {
            positiveCorrectionFactor = RivenStraightCorrectionFactorEnum.THREE_STRAIGHT_ZERO_BURDEN.getValue();
            negativeCorrectionFactor = RivenBurdenCorrectionFactorEnum.THREE_STRAIGHT_ZERO_BURDEN.getValue();
        } else if (positiveStatsQuantity == 3 && negativeStatsQuantity == 1) {
            positiveCorrectionFactor = RivenStraightCorrectionFactorEnum.THREE_STRAIGHT_ONE_BURDEN.getValue();
            negativeCorrectionFactor = RivenBurdenCorrectionFactorEnum.THREE_STRAIGHT_ONE_BURDEN.getValue();
        } else {
            throw new RuntimeException("请确认正负属性的数量是否符合");
        }
        result.put("positiveCorrectionFactor", positiveCorrectionFactor);
        result.put("negativeCorrectionFactor", negativeCorrectionFactor);
        return result;
    }

    /**
     * 计算
     * @param floatCoefficient floatCoefficient
     * @param basicValue basicValue
     * @param tendencyValue tendencyValue
     * @param correctionFactor correctionFactor
     * @return BigDecimal
     */
    private BigDecimal cal(BigDecimal floatCoefficient, BigDecimal basicValue, BigDecimal tendencyValue, BigDecimal correctionFactor) {
        return floatCoefficient.multiply(basicValue).multiply(tendencyValue).multiply(correctionFactor);
    }

    public void handler(EventReportingDTO.Message.Data data, Long groupId, Long userId, Boolean isGroup) {
        String text = data.getText();
        Map<String, Object> param = this.getParam(text, groupId, userId, isGroup);
        QueryWrapper<WmRivenItem> wmRivenItemQueryWrapper = new QueryWrapper<>();
        wmRivenItemQueryWrapper.like("item_name", param.get("rivenName"));
        List<WmRivenItem> wmRivenItemList = wmRivenItemService.list(wmRivenItemQueryWrapper);
        List<String> totalStatsNameList = new ArrayList<>();
        List<String> positiveStatsList = (List<String>) param.getOrDefault("positiveStatsList", new ArrayList<>());
        List<String> positiveStatsNameList = positiveStatsList.stream().map(p -> p.split(" ")[1]).collect(Collectors.toList());
        totalStatsNameList.addAll(positiveStatsNameList);
        Map<String, String> positiveValueMap = positiveStatsList.stream().collect(Collectors.toMap(p -> p.split(" ")[1], p -> p.split(" ")[0]));
        List<String> negativeStatsList = (List<String>) param.getOrDefault("negativeStatsList", new ArrayList<>());
        List<String> negativeStatsNameList = new ArrayList<>();
        Map<String, String> negativeValueMap = new HashMap<>();
        if (CollUtil.isNotEmpty(negativeStatsList)) {
            negativeStatsNameList = negativeStatsList.stream().map(p -> p.split(" ")[1]).collect(Collectors.toList());
            negativeValueMap = negativeStatsList.stream().collect(Collectors.toMap(p -> p.split(" ")[1], p -> p.split(" ")[0]));
        }
        totalStatsNameList.addAll(negativeStatsNameList);
        QueryWrapper<WikiRivenAttributeBasicValues> wikiRivenAttributeBasicValuesQueryWrapper = new QueryWrapper<>();
        wikiRivenAttributeBasicValuesQueryWrapper.in("name", totalStatsNameList);
        List<WikiRivenAttributeBasicValues> wikiRivenAttributeBasicValuesList = wikiRivenAttributeBasicValuesService.list(wikiRivenAttributeBasicValuesQueryWrapper);
        Map<String, WikiRivenAttributeBasicValues> wikiRivenAttributeBasicValuesMap = wikiRivenAttributeBasicValuesList.stream().collect(Collectors.toMap(WikiRivenAttributeBasicValues::getName, w -> w));
        if (wmRivenItemList.size() == 1) {
            WmRivenItem wmRivenItem = wmRivenItemList.get(0);
            int index = wmRivenItem.getUrlName().length();
            if (wmRivenItem.getUrlName().contains("_")) {
                index = wmRivenItem.getUrlName().indexOf("_");
            }
            String name = wmRivenItem.getUrlName().substring(0, 1).toUpperCase()
                    + wmRivenItem.getUrlName().substring(1, index);
            QueryWrapper<OfficialItemTendency> wmItemTendencyQueryWrapper = new QueryWrapper<>();
            wmItemTendencyQueryWrapper.like("name", name);
            List<OfficialItemTendency> wmItemTendencyList = officialItemTendencyService.list(wmItemTendencyQueryWrapper);
            Map<String, BigDecimal> correctionFactorMap = new HashMap<>();
            try {
                correctionFactorMap = this.getCorrectionFactor(positiveStatsList.size(), negativeStatsList == null ? 0:negativeStatsList.size());
            } catch (RuntimeException e) {
                QQClientUtils.sendMsg("[CQ:at,qq=" + userId + "]\n" + e.getMessage(), userId, groupId, isGroup);
            }
            BigDecimal positiveCorrectionFactor = correctionFactorMap.get("positiveCorrectionFactor");
            BigDecimal negativeCorrectionFactor = correctionFactorMap.get("negativeCorrectionFactor");
            StringBuilder sendMsg = new StringBuilder("[CQ:at,qq=" + userId + "]\n【" + param.get("rivenName") + "】分析如下：\n");
            for (OfficialItemTendency officialItemTendency : wmItemTendencyList) {
                sendMsg.append("【").append(officialItemTendency.getName()).append("】 ")
                        .append("倾向：").append(officialItemTendency.getTendency().setScale(2, RoundingMode.HALF_UP)).append("\n")
                        .append("正面属性：\n");
                positiveStatsNameList.forEach(positiveStatsName -> {
                    String positiveValue = positiveValueMap.get(positiveStatsName);
                    sendMsg.append(positiveStatsName).append("：").append(positiveValue).append("(");
                    WikiRivenAttributeBasicValues wikiRivenAttributeBasicValues = wikiRivenAttributeBasicValuesMap.get(positiveStatsName);
                    BigDecimal basicValue = null;
                    try {
                        basicValue = this.getBasicValue(wmRivenItem.getRivenType(), wikiRivenAttributeBasicValues);
                    } catch (RuntimeException e) {
                        QQClientUtils.sendMsg("[CQ:at,qq=" + userId + "]\n" + e.getMessage(), userId, groupId, isGroup);
                    }
                    BigDecimal minValue = this.cal(MIN_FLOAT_COEFFICIENT, basicValue, officialItemTendency.getTendency(), positiveCorrectionFactor).setScale(2, RoundingMode.HALF_UP);
                    BigDecimal middleValue = this.cal(MIDDLE_FLOAT_COEFFICIENT, basicValue, officialItemTendency.getTendency(), positiveCorrectionFactor).setScale(2, RoundingMode.HALF_UP);
                    BigDecimal maxValue = this.cal(MAX_FLOAT_COEFFICIENT, basicValue, officialItemTendency.getTendency(), positiveCorrectionFactor).setScale(2, RoundingMode.HALF_UP);
                    sendMsg.append(minValue).append("-").append(middleValue).append("-").append(maxValue).append(") 【");
                    if (middleValue.compareTo(new BigDecimal(positiveValue)) >= 0) {
                        sendMsg.append("低 ").append(middleValue.subtract(new BigDecimal(positiveValue)));
                    } else if (middleValue.compareTo(new BigDecimal(positiveValue)) < 0){
                        sendMsg.append("高 ").append(new BigDecimal(positiveValue).subtract(middleValue));
                    }
                    sendMsg.append("】\n");
                });
                sendMsg.append("负面属性：\n");
                Map<String, String> finalNegativeValueMap = negativeValueMap;
                negativeStatsNameList.forEach(negativeStatsName -> {
                    String negativeValue = finalNegativeValueMap.get(negativeStatsName);
                    sendMsg.append(negativeStatsName + "：").append(negativeValue).append("(");
                    WikiRivenAttributeBasicValues wikiRivenAttributeBasicValues = wikiRivenAttributeBasicValuesMap.get(negativeStatsName);
                    BigDecimal basicValue = null;
                    try {
                        basicValue = this.getBasicValue(wmRivenItem.getRivenType(), wikiRivenAttributeBasicValues);
                    } catch (RuntimeException e) {
                        QQClientUtils.sendMsg("[CQ:at,qq=" + userId + "]\n" + e.getMessage(), userId, groupId, isGroup);
                    }
                    BigDecimal minValue = BigDecimal.ZERO.subtract(this.cal(MIN_FLOAT_COEFFICIENT, basicValue, officialItemTendency.getTendency(), negativeCorrectionFactor).setScale(2, RoundingMode.HALF_UP));
                    BigDecimal middleValue = BigDecimal.ZERO.subtract(this.cal(MIDDLE_FLOAT_COEFFICIENT, basicValue, officialItemTendency.getTendency(), negativeCorrectionFactor).setScale(2, RoundingMode.HALF_UP));
                    BigDecimal maxValue = BigDecimal.ZERO.subtract(this.cal(MAX_FLOAT_COEFFICIENT, basicValue, officialItemTendency.getTendency(), negativeCorrectionFactor).setScale(2, RoundingMode.HALF_UP));
                    sendMsg.append(minValue).append("-").append(middleValue).append("-").append(maxValue).append(") 【");
                    if (middleValue.compareTo(new BigDecimal(negativeValue)) >= 0) {
                        sendMsg.append("低 ").append(middleValue.subtract(new BigDecimal(negativeValue)));
                    } else if (middleValue.compareTo(new BigDecimal(negativeValue)) < 0){
                        sendMsg.append("高 ").append(new BigDecimal(negativeValue).subtract(middleValue));
                    }
                    sendMsg.append("】\n");
                });
                sendMsg.append("----------------------------\n");
            }
            QQClientUtils.sendMsg(sendMsg.toString(), userId, groupId, isGroup);
            qGroupUserOperationQuantityService.statisticsOperationQuantityByType(groupId, userId, OperationTypeEnum.RA);
        }
    }

    private Map<String, Object> getParam(String text, Long groupId, Long userId, Boolean isGroup) {
        Map<String, Object> result = new HashMap<>();
        String[] wmlS = text.replace("ra ", "").split("] \\[");
        String rivenName = null;
        List<String> positiveStatsList = null;
        List<String> negativeStatsList = null;
        for (int i = 0; i < wmlS.length; i++) {
            // 第一位是名称
            if (i == 0 && StrUtil.isNotEmpty(wmlS[i].replace("[", "").replace("]", ""))) {
                rivenName = wmlS[i].replace("[", "").replace("]", "");
            }
            // 第二位是正向属性
            if (i == 1 && StrUtil.isNotEmpty(wmlS[i].replace("[", "").replace("]", ""))) {
                positiveStatsList = Arrays.asList(wmlS[i].replace("[", "").replace("]", "").split(","));
                if (positiveStatsList.size() > 3) {
                    QQClientUtils.sendMsg("[CQ:at,qq=" + userId + "]\n正面属性最多只能输入三个\n", userId, groupId, isGroup);
                }
            }
            // 第三位是反向属性
            if (i == 2 && StrUtil.isNotEmpty(wmlS[i].replace("[", "").replace("]", ""))) {
                negativeStatsList = Arrays.asList(wmlS[i].replace("[", "").replace("]", "").split(","));
                if (negativeStatsList.size() > 3) {
                    QQClientUtils.sendMsg("[CQ:at,qq=" + userId + "]\n反面属性最多只能输入三个\n", userId, groupId, isGroup);
                }
            }
        }
        result.put("rivenName", rivenName);
        result.put("positiveStatsList", positiveStatsList);
        result.put("negativeStatsList", negativeStatsList);
        return result;
    }
}
